package com.jz.community.modulemine.information.task;

import android.app.Activity;

import com.jz.community.basecomm.bean.UserInfo;
import com.jz.community.basecomm.constant.Constant;
import com.jz.community.basecomm.net.OkHttpUtil;
import com.jz.community.basecomm.task.ITaskCallbackListener;
import com.jz.community.basecomm.task.RxTask;
import com.jz.community.basecomm.utils.BaseSpUtils;
import com.jz.community.basecomm.utils.ConverterUtils;
import com.jz.community.basecomm.utils.JsonUtils;
import com.jz.community.basecomm.utils.Preconditions;
import com.jz.community.commview.view.widget.ProgressDialogManager;

import java.util.HashMap;

/**
 * Created by Tony.Fan on 2018/5/8 16:50
 */
public class AddUserAvatarTask extends RxTask<String, Integer, UserInfo> {
    private Activity activity;
    private ITaskCallbackListener taskListener;

    public AddUserAvatarTask(Activity activity,
                             ITaskCallbackListener taskListener) {
        super(activity);
        this.activity = activity;
        this.taskListener = taskListener;
    }

    @Override
    protected UserInfo doInBackground(String... params) {
        UserInfo info = null;

        // 执行请求
        String result = OkHttpUtil.post(Constant.UPDATE_USER_AVATAR_URL, getParam(params[0]));

        if (!Preconditions.isNullOrEmpty(result)) {
            info = JsonUtils.parseObject(result, UserInfo.class);
        }
        return info;
    }

    @Override
    protected void onPostExecute(UserInfo result) {
        // 执行结果回调函数
        taskListener.doTaskComplete(result);
        super.onPostExecute(result);
    }

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
    }

    @Override
    protected void onCancelled() {
        super.onCancelled();
    }

    private HashMap<String, Object> getParam(String icon) {
        HashMap<String, Object> params = new HashMap<>();
        params.put("userId", BaseSpUtils.getInstance().getUserId(activity));
        params.put("icon", icon);
        params.put("isDefault", 0);
        return params;
    }
}
